package com.tledu.spring.controller;

import com.tledu.spring.core.entity.AjaxResult;
import com.tledu.spring.core.tools.JudgmentAuthority;
import com.tledu.spring.core.vo.BasicVo;
import com.tledu.spring.domain.Role;
import com.tledu.spring.service.IRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/2 21:19
 */
@Controller
@RequestMapping("/role")
@AllArgsConstructor
public class RoleController {
    private final IRoleService roleService;
    private final String allAuto = "角色新增,角色修改,角色删除,用户新增,用户修改,用户删除,员工新增,员工修改,员工删除,部门新增," +
            "部门修改,部门删除,工艺新增,工艺修改,工艺删除,工艺计划新增,工艺计划修改,工艺计划删除,工艺要求新增,工艺要求修改,工艺要求删除," +
            "物料新增,物料修改,物料删除,物料收入新增,物料收入修改,物料收入删除,物料消耗新增,物料消耗修改,物料消耗删除";
    /**
     * ①这里需要写,放在controller里面
     */
    private JudgmentAuthority judgmentAuthority;
    /**
     * 展示
     * @return 展示
     */
    @RequestMapping("listBefore")
    public String list(){
        return "role/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public BasicVo list(Integer page, Integer limit){
        return roleService.selectByAll(page,limit);
    }

    /**
     * 添加数据
     * @return 添加
     */
    @RequestMapping("addBefore")
    public String add(Model model, HttpServletRequest request){
        if (judgmentAuthority.JudgmentAuthorityX(request).contains("角色新增")){
            String[] strs = allAuto.split(",");
            List autoList = Arrays.asList(strs);
            model.addAttribute("autoList",autoList);
            return "role/add";
        }
        return "/permissionDenied";
    }
    @RequestMapping("add")
    @ResponseBody
    public AjaxResult<String> add(Role role){
        int m = roleService.insertSelective(role);
        if (m==1){
            return new AjaxResult<>("添加成功");
        }
        return new AjaxResult<>("添加失败");
    }

    /**
     * 修改
     * @param model
     * @param r_id
     * @return
     */
    @RequestMapping("updateBefore")
    public String update(Model model,Integer r_id,HttpServletRequest request){
        if (judgmentAuthority.JudgmentAuthorityX(request).contains("角色修改")){
            Role role1 = roleService.selectById(r_id);
            model.addAttribute("idRole",role1);
            String[] strs = role1.getR_auto().split(",");
            List<String> autoList = Arrays.asList(strs);
            model.addAttribute("autoList",autoList);
            String[] strs1 = allAuto.split(",");
            List<String> autoList1 = Arrays.asList(strs1);
            List arrList = new ArrayList(autoList);
            List arrList1 = new ArrayList(autoList1);
            // 使用asList不能用removeAll,需要先变成List
            arrList1.removeAll(arrList);
            model.addAttribute("autoNewList",arrList1);
            return "role/update";
        }
        return "/permissionDenied";
    }
    @PostMapping("updateAfter")
    @ResponseBody
    public AjaxResult<String> update(Role role){
        int m = roleService.updateByR_id(role,role.getR_id());
        if (m==1){
            return new AjaxResult<>("添加成功");
        }
        return new AjaxResult<>("添加失败");
    }

    /**
     * 删除
     * @param r_id
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult<String > delete(String r_id,HttpServletRequest request){
        if (judgmentAuthority.JudgmentAuthorityX(request).contains("角色删除")){
            int m = roleService.deleteByR_id(r_id);
            if (m!=1){
                return new AjaxResult<>("删除失败");
            }
            return new AjaxResult<>("删除成功");
        }
        return new AjaxResult<>("删除失败,您可能没有权限");
    }

}
